package com.to8to.scene.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.QueryOperators;
import com.to8to.commons.mongo.MongoClientBase;
import com.to8to.commons.utils.Progress;
import com.to8to.commons.utils.StringUtil;
import com.to8to.commons.utils.ThriftUtil;
import com.to8to.scene.server.Commons;
import com.to8to.scene.server.Tables;
import com.to8to.thrift.diary.consts.constConstants;
import com.to8to.thrift.diary.module.TComment;
import com.to8to.thrift.diary.module.TDiary;

/**
 * FixCommentRefCommentId.java
 * 作 者:alpha.ren
 * 版 本:v2.0
 * 日期:2015年01月26日
 * 描 述:修正comment ref_comment_id字段
 */
public class FixCommentRefCommentId implements DataFixIface
{
    public static Logger logger = LoggerFactory.getLogger(FixCommentRefCommentId.class);

    static Connection conn;

    public void fix(Connection mysql, MongoClientBase mongo) throws SQLException
    {
        DBObject query = new BasicDBObject();

        query.put(TComment._Fields.TGT_TYPE.getFieldName(), constConstants.TARGET_TYPE_COMMENT);
        
        DBCursor comment_cur = Commons.mongo.cursor(Tables.Comment, query, null, new BasicDBObject(TDiary._Fields.CTIME.getFieldName(), -1), 0, 0);

        int i = 0;
        logger.info("{} origin comments' sub comment need tobe updated!", comment_cur.count());
        Progress progress = new Progress(comment_cur.count());
        while (comment_cur.hasNext())
        {
            DBObject sub_comment_db = comment_cur.next();
            if (null == sub_comment_db)
                continue;
            TComment sub_comment = ThriftUtil.dbObject2ThriftObject(sub_comment_db, TComment.class);
            progress.increase(logger);

            String sub_comment_id = sub_comment.get_id();
            String sub_origin_ref_comment_id = sub_comment.getRef_comment_id();
            
            TComment ref_comment =null;
            
            do{            	
            	String ref_comment_id = sub_comment.getRef_comment_id();
            	
            	DBObject ref_comment_db = mongo.findById(Tables.Comment, ref_comment_id);
            	ref_comment = ThriftUtil.dbObject2ThriftObject(ref_comment_db, TComment.class);
            	if(null!=ref_comment)
            		sub_comment = ref_comment;
            }while(null!=ref_comment);

            //最后一个sub_comment就是找到的根ref_comment;
            mongo.updateField(Tables.Comment, sub_comment_id, TComment._Fields.REF_COMMENT_ID.getFieldName(),sub_comment.get_id());
            mongo.updateField(Tables.Comment, sub_comment_id, TComment._Fields.REF_COMMENT_USER_ID.getFieldName(),sub_comment.getUser_id());
            logger.debug("update comment {} on ref_comment_id from {}",sub_comment_id,sub_origin_ref_comment_id);
            logger.debug("update comment {} on ref_comment_id for {}",sub_comment_id,sub_comment.get_id());
            i++;
        }
        logger.info("{} records updated!", i);
    }

    public static void main(String[] args) throws SQLException, ConfigurationException
    {
//        PropertiesConfiguration pc = new PropertiesConfiguration("common.properties");

        Commons.init(null);

        conn =null;

        (new FixCommentRefCommentId()).fix(conn, Commons.mongo);
        
        System.exit(0);
    }
}
